﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Gencode.PrsCommon.App
{
    /// <summary>
    /// Sets different conversions with dates
    /// </summary>
    public class DateUtil
    {
        /// <summary>
        /// Converts a date to SQL converted date
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string ConvertDateToSQL(string date)
        {
			if (date == string.Empty)
				date = "01.01.1900";

            return String.Format("{0}{1}{2}{3}{4}{5}{6}{7}", date[6], date[7], date[8], date[9], date[3], date[4], date[0], date[1]);
        }

        public static string ConvertSqlToDate(string date)
        {
			if (date == string.Empty)
				date = "01.01.1900";

            return String.Format("{0}{1}.{2}{3}.{4}{5}{6}{7}", date[0], date[1], date[3], date[4], date[6], date[7], date[8], date[9]);
        }

        public static string GetYear(string date)
        {
            return String.Format("{0}{1}{2}{3}", date[6], date[7], date[8], date[9]);
        }

        public static string GetMonth(string date)
        {
            return String.Format("{0}{1}", date[3], date[4]);
        }

        public static string GetDay(string date)
        {
            return String.Format("{0}{1}", date[0], date[1]);
        }

		public static string DateBuilder(int day, int month, int year)
		{
			string d;
			if (day < 10)
				d = "0" + day.ToString();
			else
				d = day.ToString();
			string m;
			if (month < 10)
				m = "0" + month.ToString();
			else
				m = month.ToString();

			return d + "." + m + "." + year.ToString();
		}

		public static int ToInt(string date)
		{
			if (date == string.Empty)
				date = "01.01.1900";

			return int.Parse(String.Format("{0}{1}{2}{3}{4}{5}{6}{7}", date[6], date[7], date[8], date[9], date[3], date[4], date[0], date[1]));
		}

		public static string FirstDateNextMonth()
		{
			string date = "";
			if (DateTime.Today.Month == 12)
			{
				date = "01." + "0" + Convert.ToString(Convert.ToInt16(DateTime.Today.Month) - 11) + "." + (DateTime.Today.Year + 1);
			}
			else
			{
				if (DateTime.Today.Month < 9)
				{
					date = "01." + "0" + Convert.ToString(Convert.ToInt16(DateTime.Today.Month) + 1) + "." + DateTime.Today.Year;
				}
				else
				{
					date = "01." + Convert.ToString(Convert.ToInt16(DateTime.Today.Month) + 1) + "." + DateTime.Today.Year;
				}
			}
			return date;
		}

		public static string LastDateNextMonth()
		{
			int month = 0;
			int year = 0;
			string date = "";
			if (DateTime.Today.Month == 12)
			{
				month = Convert.ToInt16(DateTime.Today.Month) - 11;
				year = Convert.ToInt16(DateTime.Today.Year) + 1;
			}
			else
			{
				month = Convert.ToInt16(DateTime.Today.Month) + 1;
				year = Convert.ToInt16(DateTime.Today.Year);
			}
			if (month < 10)
			{
				date = DateTime.DaysInMonth(year, month) + ".0" + month.ToString() + "." + year.ToString();
			}
			else
			{
				date = DateTime.DaysInMonth(year, month) + "." + month.ToString() + "." + year.ToString();
			}
			return date;
		}

        public static string DayInWeek(int year, int month, int day)
        {
            string d = "";
            DateTime dt = new DateTime(year, month, day);
            switch (dt.DayOfWeek)
            {
                case DayOfWeek.Monday:
                    d = "MO";
                    break;
                case DayOfWeek.Tuesday:
                    d = "DI";
                    break;
                case DayOfWeek.Wednesday:
                    d = "MI";
                    break;
                case DayOfWeek.Thursday:
                    d = "DO";
                    break;
                case DayOfWeek.Friday:
                    d = "FR";
                    break;
                case DayOfWeek.Saturday:
                    d = "SA";
                    break;
                case DayOfWeek.Sunday:
                    d = "So";
                    break;
            }
            return d;
        }
    }
}
